package com.flc.gateway.server;

import com.flc.gateway.codc.CommandEncoder;
import com.flc.gateway.codc.MessageDecoder;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler;

/**
 * @author haizi
 * @date 2017/12/31
 */
public class ServerInitializer extends ChannelInitializer<SocketChannel> {

    private final int heartBeat;

    public ServerInitializer(int heartBeat){
        this.heartBeat = heartBeat;
    }

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline
                .addLast("decode", new MessageDecoder())
                .addLast("encode", new CommandEncoder())
                .addLast(new ServerHandler());

        if (heartBeat != 0) {
            pipeline.addLast(new IdleStateHandler(0, 0, heartBeat));
        }

    }
}
